<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
          <!--<el-form-item label="主键">
            <el-input v-model="form.id" style="width: 370px;" />
          </el-form-item>-->
          <el-form-item label="型号" prop="model">
            <el-input v-model="form.model" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="颜色" prop="color">
            <el-input v-model="form.color" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="尺码" prop="size">
            <el-select v-model="form.size" clearable size="small" class="filter-item" style="width: 370px">
              <el-option v-for="item in dict.size" :key="item.key" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="男女款" prop="style" style="width: 370px;">
            <el-select v-model="form.style" clearable size="small" class="filter-item" style="width: 370px">
              <el-option v-for="item in dict.style" :key="item.key" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="总库存数量" style="width: 370px;" prop="totalQuantity">
            <el-input v-model="form.totalQuantity" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="售出数量" prop="soldQuantity">
            <el-input v-model="form.soldQuantity" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        :row-class-name="tableRowClassName"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="model" label="型号" />
        <el-table-column prop="color" label="颜色" />
        <el-table-column prop="size" label="尺码" />
        <el-table-column prop="style" label="男女款" />
        <el-table-column prop="totalQuantity" label="总库存数量" />
        <el-table-column prop="soldQuantity" label="售出数量" />
        <el-table-column prop="stockQuantity" label="在库数量">
          <template slot-scope="scope">
            <span>{{ scope.row.totalQuantity - scope.row.soldQuantity }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','stock:edit','stock:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudStock from '@/api/order/stock'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, model: null, color: null, size: null, style: null, totalQuantity: null, soldQuantity: null }
export default {
  name: 'Stock',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  // 设置数据字典
  dicts: ['size', 'style'],
  cruds() {
    return CRUD({ title: '库存', url: 'api/stock', sort: 'id,asc', crudMethod: { ...crudStock }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'stock:add'],
        edit: ['admin', 'stock:edit'],
        del: ['admin', 'stock:del']
      },
      rules: {
        totalQuantity: [
          { required: true, message: '请输入总库存数量', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'model', display_name: '型号' },
        { key: 'color', display_name: '颜色' },
        { key: 'size', display_name: '尺码' },
        { key: 'style', display_name: '男女款' }
      ]
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }
      return true
    },
    tableRowClassName: function({ row, rowIndex }) {
      if (row.totalQuantity - row.soldQuantity === 0) {
        return 'warning-row'
      } else if (row.totalQuantity - row.soldQuantity < 0) {
        return 'error-row'
      }
      return ''
    }
  }
}
</script>

<style>
  .el-table .warning-row {
    background: LightGray;
  }

  .el-table .error-row {
    background: Crimson;
  }
</style>
